﻿using System;
using System.Windows.Forms;
using HRM.HRMServiceProxy;

namespace HRM
{
    public partial class frmInterestRateInfo : Form
    {
        bool isChange = false;
        InterestRateInfo _InterestRateInfo;
        InterestRateClient _wcfInterest;
        DialogResult dlgResult;

        int _InterestRateID;

        public frmInterestRateInfo(int InterestRateID)
        {
            InitializeComponent();
            //LoadLanguage();
            dlgResult = DialogResult.Cancel;
            _InterestRateInfo = new InterestRateInfo();
            _wcfInterest = Service.InterestRate();
            _InterestRateID = InterestRateID;
            if (_InterestRateID > 0)
            {
                _InterestRateInfo = _wcfInterest.InterestRate_GetInfo(_InterestRateID);
                Info2Form();
            }
        }
        void LoadLanguage()
        {
            if (this.Tag != null)
                this.Text = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
            UpdateUI.Load_Panel(ref panel2);
        }

        void Info2Form()
        {
            txtCode.Text = _InterestRateInfo.Code;
            txtFromDate.Text = _InterestRateInfo.FromDate.ToString("dd/MM/yyyy");
            txtToDate.Text = _InterestRateInfo.ToDate.ToString("dd/MM/yyyy");
            txtRate.Text = _InterestRateInfo.Percent.ToString();
            txtDescription.Text = _InterestRateInfo.Description;
            isChange = false;
        }

        void Form2Info()
        {
            _InterestRateInfo.ID = _InterestRateID;
            _InterestRateInfo.Code = txtCode.Text;
            _InterestRateInfo.FromDate = DateTime.ParseExact(txtFromDate.Text, "dd/MM/yyyy", null);
            _InterestRateInfo.ToDate = DateTime.ParseExact(txtToDate.Text, "dd/MM/yyyy", null);
            _InterestRateInfo.Percent = Convert.ToDecimal(txtRate.Text);
            _InterestRateInfo.Description = txtDescription.Text;
        }

        bool CheckValid()//kiểm tra dữ liệu nhập
        {
            DateTime dateVal;
            if (!DateTime.TryParse(txtFromDate.Text, out dateVal))
            {
                txtFromDate.Focus();
                MessageBox.Show(UpdateUI.GetString("ngaybatdaukhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (!DateTime.TryParse(txtToDate.Text, out dateVal))
            {
                txtToDate.Focus();
                MessageBox.Show(UpdateUI.GetString("ngayketthuckhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckValid())
            {
                isChange = true;
                return;
            }
            Form2Info();
            if (_InterestRateID > 0)//update
            {
                if (_wcfInterest.InterestRate_Update(_InterestRateInfo))
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }
            else//insert
            {
                _InterestRateInfo.ID = _wcfInterest.InterestRate_Insert(_InterestRateInfo);
                if (_InterestRateInfo.ID > 0)
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }

        }

        private void btnNew_Click(object sender, EventArgs e)
        {           
            if (_InterestRateInfo.ID == 0)
            {
                MessageBox.Show("Bạn muốn lưu trước khi tạo mới");
                return;
            }
            _InterestRateInfo = new InterestRateInfo();
            _InterestRateInfo.ID = 0;
            txtCode.Text = "";
            txtDescription.Text = "";
            txtFromDate.Text = "";
            txtRate.Text = "";
            txtToDate.Text = "";

        }

        private void frmInterestRateInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isChange)
            {
                if (MessageBox.Show("Dữ liệu của bạn đã thay đổi.\nBạn có muốn lưu trước khi thoát không?.", UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    btnSave_Click(null, null);
                    if (isChange)
                        e.Cancel = true;
                    else
                        e.Cancel = false;
                }
            }
            _wcfInterest.Close();
            this.DialogResult = dlgResult;
        }

        private void frmInterestRateInfo_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    if (!e.Alt && !e.Control)
                    {
                        this.ProcessTabKey(!e.Shift);
                        e.Handled = true;
                    }
                    break;
                case Keys.Escape:
                    this.Close();
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnSave_Click(null, null);
                    break;
            }
        }

        private void DataChange(object sender, EventArgs e)
        {
            isChange = true;
        }
    }
}
